home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / domacnost a kancelar / joomla / Joomla_1.5.4-Stable-Full_Package.exe / includes / application.php next >
PHP Script  |  2008-07-06  |  8KB  |  376 lines

  1. <?php
  2. /**
  3. * @version        $Id: application.php 10382 2008-06-01 06:56:02Z pasamio $
  4. * @package        Joomla
  5. * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  6. * @license        GNU/GPL, see LICENSE.php
  7. * Joomla! is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13.  
  14. // no direct access
  15. defined( '_JEXEC' ) or die( 'Restricted access' );
  16.  
  17. jimport('joomla.application.component.helper');
  18.  
  19. /**
  20. * Joomla! Application class
  21. *
  22. * Provide many supporting API functions
  23. *
  24. * @package        Joomla
  25. * @final
  26. */
  27. class JSite extends JApplication
  28. {
  29.     /**
  30.     * Class constructor
  31.     *
  32.     * @access protected
  33.     * @param    array An optional associative array of configuration settings.
  34.     * Recognized key values include 'clientId' (this list is not meant to be comprehensive).
  35.     */
  36.     function __construct($config = array())
  37.     {
  38.         $config['clientId'] = 0;
  39.         parent::__construct($config);
  40.     }
  41.  
  42.     /**
  43.     * Initialise the application.
  44.     *
  45.     * @access public
  46.     */
  47.     function initialise( $options = array())
  48.     {
  49.         // if a language was specified it has priority
  50.         // otherwise use user or default language settings
  51.         if (empty($options['language']))
  52.         {
  53.             $user = & JFactory::getUser();
  54.             $lang    = $user->getParam( 'language' );
  55.  
  56.             // Make sure that the user's language exists
  57.             if ( $lang && JLanguage::exists($lang) ) {
  58.                 $options['language'] = $lang;
  59.             } else {
  60.                 $params =  JComponentHelper::getParams('com_languages');
  61.                 $client    =& JApplicationHelper::getClientInfo($this->getClientId());
  62.                 $options['language'] = $params->get($client->name, 'en-GB');
  63.             }
  64.  
  65.         }
  66.  
  67.         // One last check to make sure we have something
  68.         if ( ! JLanguage::exists($options['language']) ) {
  69.             $options['language'] = 'en-GB';
  70.         }
  71.  
  72.         parent::initialise($options);
  73.     }
  74.  
  75.     /**
  76.     * Route the application
  77.     *
  78.     * @access public
  79.     */
  80.     function route() {
  81.         parent::route();
  82.     }
  83.  
  84.     /**
  85.     * Dispatch the application
  86.     *
  87.     * @access public
  88.     */
  89.     function dispatch($component)
  90.     {
  91.         $document    =& JFactory::getDocument();
  92.         $user        =& JFactory::getUser();
  93.         $router     =& $this->getRouter();
  94.         $params     =& $this->getParams();
  95.  
  96.         switch($document->getType())
  97.         {
  98.             case 'html':
  99.             {
  100.                 //set metadata
  101.                 $document->setMetaData( 'keywords', $this->getCfg('MetaKeys') );
  102.  
  103.                 if ( $user->get('id') ) {
  104.                     $document->addScript( JURI::root(true).'/includes/js/joomla.javascript.js');
  105.                 }
  106.  
  107.                 if($router->getMode() == JROUTER_MODE_SEF) {
  108.                     $document->setBase(JURI::current());
  109.                 }
  110.             } break;
  111.  
  112.             case 'feed':
  113.             {
  114.                 $document->setBase(JURI::current());
  115.             } break;
  116.  
  117.             default: break;
  118.         }
  119.  
  120.  
  121.         $document->setTitle( $params->get('page_title') );
  122.         $document->setDescription( $params->get('page_description') );
  123.  
  124.         $contents = JComponentHelper::renderComponent($component);
  125.         $document->setBuffer( $contents, 'component');
  126.     }
  127.  
  128.     /**
  129.     * Display the application.
  130.     *
  131.     * @access public
  132.     */
  133.     function render()
  134.     {
  135.         $document =& JFactory::getDocument();
  136.         $user     =& JFactory::getUser();
  137.  
  138.         // get the format to render
  139.         $format = $document->getType();
  140.  
  141.         switch($format)
  142.         {
  143.             case 'feed' :
  144.             {
  145.                 $params = array();
  146.             } break;
  147.  
  148.             case 'html' :
  149.             default     :
  150.             {
  151.                 $template    = $this->getTemplate();
  152.                 $file         = JRequest::getCmd('tmpl', 'index');
  153.  
  154.                 if ($this->getCfg('offline') && $user->get('gid') < '23' ) {
  155.                     $file = 'offline';
  156.                 }
  157.                 if (!is_dir( JPATH_THEMES.DS.$template ) && !$this->getCfg('offline')) {
  158.                     $file = 'component';
  159.                 }
  160.                 $params = array(
  161.                     'template'     => $template,
  162.                     'file'        => $file.'.php',
  163.                     'directory'    => JPATH_THEMES
  164.                 );
  165.             } break;
  166.          }
  167.  
  168.         $data = $document->render( $this->getCfg('caching'), $params);
  169.         JResponse::setBody($data);
  170.     }
  171.  
  172.    /**
  173.     * Login authentication function
  174.     *
  175.     * @param    array     Array( 'username' => string, 'password' => string )
  176.     * @param    array     Array( 'remember' => boolean )
  177.     * @access public
  178.     * @see JApplication::login
  179.     */
  180.     function login($credentials, $options = array())
  181.     {
  182.          //Set the application login entry point
  183.          if(!array_key_exists('entry_url', $options)) {
  184.              $options['entry_url'] = JURI::base().'index.php?option=com_user&task=login';
  185.          }
  186.  
  187.         return parent::login($credentials, $options);
  188.     }
  189.  
  190.     /**
  191.     * Check if the user can access the application
  192.     *
  193.     * @access public
  194.     */
  195.     function authorize($itemid)
  196.     {
  197.         $menus    =& JSite::getMenu();
  198.         $user    =& JFactory::getUser();
  199.         $aid    = $user->get('aid');
  200.  
  201.         if(!$menus->authorize($itemid, $aid))
  202.         {
  203.             if ( ! $aid )
  204.             {
  205.                 // Redirect to login
  206.                 $uri        = JFactory::getURI();
  207.                 $return        = $uri->toString();
  208.  
  209.                 $url  = 'index.php?option=com_user&view=login';
  210.                 $url .= '&return='.base64_encode($return);;
  211.  
  212.                 //$url    = JRoute::_($url, false);
  213.                 $this->redirect($url, JText::_('You must login first') );
  214.             }
  215.             else
  216.             {
  217.                 JError::raiseError( 403, JText::_('Not Authorised') );
  218.             }
  219.         }
  220.     }
  221.  
  222.     /**
  223.      * Get the appliaction parameters
  224.      *
  225.      * @param    string    The component option
  226.      * @return    object    The parameters object
  227.      * @since    1.5
  228.      */
  229.     function &getParams($option = null)
  230.     {
  231.         static $params;
  232.  
  233.         if (!is_object($params))
  234.         {
  235.             // Get component parameters
  236.             if (!$option) {
  237.                 $option = JRequest::getCmd('option');
  238.             }
  239.             $params = &JComponentHelper::getParams($option);
  240.  
  241.             // Get menu parameters
  242.             $menus    = &JSite::getMenu();
  243.             $menu    = $menus->getActive();
  244.  
  245.             $title       = htmlspecialchars_decode($this->getCfg('sitename' ));
  246.             $description = $this->getCfg('MetaDesc');
  247.  
  248.             // Lets cascade the parameters if we have menu item parameters
  249.             if (is_object($menu))
  250.             {
  251.                 $params->merge(new JParameter($menu->params));
  252.                 $title = $menu->name;
  253.  
  254.             }
  255.  
  256.             $params->def( 'page_title'      , $title );
  257.             $params->def( 'page_description', $description );
  258.         }
  259.  
  260.         return $params;
  261.     }
  262.  
  263.     /**
  264.      * Get the appliaction parameters
  265.      *
  266.      * @param    string    The component option
  267.      * @return    object    The parameters object
  268.      * @since    1.5
  269.      */
  270.     function &getPageParameters( $option = null )
  271.     {
  272.         return $this->getParams( $option );
  273.     }
  274.  
  275.     /**
  276.      * Get the template
  277.      *
  278.      * @return string The template name
  279.      * @since 1.0
  280.      */
  281.     function getTemplate()
  282.     {
  283.         // Allows for overriding the active template from a component, and caches the result of this function
  284.         // e.g. $mainframe->setTemplate('solar-flare-ii');
  285.         if ($template = $this->get('setTemplate')) {
  286.             return $template;
  287.         }
  288.  
  289.         // Get the id of the active menu item
  290.         $menu =& JSite::getMenu();
  291.         $item = $menu->getActive();
  292.  
  293.         $id = 0;
  294.         if(is_object($item)) { // valid item retrieved
  295.             $id = $item->id;
  296.         }
  297.  
  298.         // Load template entries for the active menuid and the default template
  299.         $db =& JFactory::getDBO();
  300.         $query = 'SELECT template'
  301.             . ' FROM #__templates_menu'
  302.             . ' WHERE client_id = 0 AND (menuid = 0 OR menuid = '.(int) $id.')'
  303.             . ' ORDER BY menuid DESC'
  304.             ;
  305.         $db->setQuery($query, 0, 1);
  306.         $template = $db->loadResult();
  307.  
  308.         // Allows for overriding the active template from the request
  309.         $template = JRequest::getCmd('template', $template);
  310.         $template = JFilterInput::clean($template, 'cmd'); // need to filter the default value as well
  311.  
  312.         // Fallback template
  313.         if (!file_exists(JPATH_THEMES.DS.$template.DS.'index.php')) {
  314.             $template = 'rhuk_milkyway';
  315.         }
  316.  
  317.         // Cache the result
  318.         $this->set('setTemplate', $template);
  319.         return $template;
  320.     }
  321.  
  322.     /**
  323.      * Overrides the default template that would be used
  324.      *
  325.      * @param string The template name
  326.      */
  327.     function setTemplate( $template )
  328.     {
  329.         if (is_dir(JPATH_THEMES.DS.$template)) {
  330.             $this->set('setTemplate', $template);
  331.         }
  332.     }
  333.  
  334.     /**
  335.      * Return a reference to the JPathway object.
  336.      *
  337.      * @access public
  338.      * @return object JPathway.
  339.      * @since 1.5
  340.      */
  341.     function &getMenu()
  342.     {
  343.         $options = array();
  344.         $menu =& parent::getMenu('site', $options);
  345.         return $menu;
  346.     }
  347.  
  348.     /**
  349.      * Return a reference to the JPathway object.
  350.      *
  351.      * @access public
  352.      * @return object JPathway.
  353.      * @since 1.5
  354.      */
  355.     function &getPathWay()
  356.     {
  357.         $options = array();
  358.         $pathway =& parent::getPathway('site', $options);
  359.         return $pathway;
  360.     }
  361.  
  362.     /**
  363.      * Return a reference to the JRouter object.
  364.      *
  365.      * @access    public
  366.      * @return    JRouter.
  367.      * @since    1.5
  368.      */
  369.     function &getRouter()
  370.     {
  371.         $config =& JFactory::getConfig();
  372.         $options['mode'] = $config->getValue('config.sef');
  373.         $router =& parent::getRouter('site', $options);
  374.         return $router;
  375.     }
  376. }